package com.yuchengtech.simpleServer.action;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import net.sf.json.JSONObject;
import com.wox.action.JsonData;
import com.wox.action.WoxAction;
import com.wox.util.BindResource;

public class SaveBindResourceAction extends WoxAction {


	private String bindId;
	private String keyMapStr;

	@Override
	public String excute() throws Exception {
		
		JsonData jsonData=getJsonData();
		Properties pro=null;
		try{
		if(bindId!=null)
			{
			 pro=BindResource.getBindProperties(bindId);
			 if(pro==null)
			 {
				 pro=new Properties();
			 }

				JSONObject jsonObject=JSONObject.fromObject(keyMapStr);
				HashMap map=(HashMap) JSONObject.toBean(jsonObject, HashMap.class);
				
				//pro.putAll(map);
				Set<String> keySet=map.keySet();
				for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
					String key = (String) iterator.next();
					pro.setProperty(key,(String) map.get(key));
				}
				boolean b=BindResource.saveBindProperties(bindId, pro);
				jsonData.setState(b);
				if(b)
					jsonData.setMessage("save success!");
				else 
					jsonData.setMessage("save failed!");
			}else
			{
				jsonData.setMessage("save failed!");
			}
		}catch (Exception e) {
			jsonData.setState(false);
			jsonData.setMessage(e.getMessage());
		}
		return JSONBACK;
	
		
	}

	public void setBindId(String bindId) {
		this.bindId = bindId;
	}

	public String getBindId() {
		return bindId;
	}

	
	public String getKeyMapStr() {
		return keyMapStr;
	}

	public void setKeyMapStr(String keyMapStr) {
		this.keyMapStr = keyMapStr;
	}

	
	/*
	public static void main(String[] args) {
		Properties pro=new Properties();
		pro.setProperty("good", "test1");
		String str="{\"sdfdf\":\"sdfkldf\",\"good\":\"test\",\"dfls\":\"dfldfs\",\"fsklsdf\":\"tlfdslfd\"}";
		JSONObject jsonObject=JSONObject.fromObject(str);
		HashMap map=(HashMap) JSONObject.toBean(jsonObject, HashMap.class);
		pro.putAll(map);
		System.out.println(pro);
	}
	*/
}
